package com.dream.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Component;

import com.dream.common.Common;
import com.dream.common.JsonString;
import com.dream.common.SuperDao;
import com.dream.model.SysUser;
import com.dream.model.Sys_Class;
import com.dream.model.Sys_Lable;
import com.dream.model.Sys_LearnPlant;
import com.dream.model.Sys_User_Lable;
import com.dream.system.service.ISysUser;

@Component("sysUserImpl")
@SuppressWarnings("all")
public class SysUserImpl extends SuperDao<Object> implements ISysUser {

	/**
	 * 增加和更新用户
	 */
	@Override
	public int addUser(SysUser model) {
		// TODO Auto-generated method stub

		String hql = "from SysUser t where t.userName =  ? or t.tell = ? ";
		Object[] object = new Object[] { model.getUserName(), model.getTell() };
		@SuppressWarnings("unchecked")
		List<SysUser> list = this.getList(hql, object, 0, 0, "");
		if (null == list || list.size() <= 0) {
			model.setShowTime(Common.getTime());
			model.setCreateTime(Common.getTime1());
			this.addT(model);
			return 1;
		} else {
			if (null == model.getTable_id() || "".equals(model.getTable_id())) {
				// 证明有相同名称和电话的
				return 2;
			} else {

				for (int i = 0; i < list.size(); i++) {
					SysUser su = list.get(i);
					if (su.getTable_id().equals(model.getTable_id())) {
						// su.setDesc(model.getDesc());
						su.setNote(model.getNote());
						su.setSex(model.getSex());
						// su.setTeacherId(model.getTeacherId());
						// su.setTeacherType(model.getTeacherType());
						su.setTell(model.getTell());
						su.setUserEmail(model.getUserEmail());
						su.setUserEngListName(model.getUserEngListName());
						if (null != model.getUserImage()) {
							su.setUserImage(model.getUserImage());
						}
						su.setUserName(model.getUserName());
						su.setUserShowName(model.getUserShowName());
						su.setUserType(model.getUserType());
						su.setUserPwd(model.getUserPwd());

						this.updateT(su);
						return 1;
					}
				}
			}
		}

		return 0;
	}

	/**
	 * 按条件查询用户
	 */
	@SuppressWarnings("all")
	@Override
	public Map getAllUser(String userType1, String userType2, SysUser model) {
		// TODO Auto-generated method stub
		String hsql = "from SysUser t ";
		String orderby = "";

		String wheresql = "";
		List<Object> list = new ArrayList<Object>();
		// 按名称
		if (null != model.getUserName() && !"".equals(model.getUserName())) {
			if (wheresql.length() > 3) {
				wheresql += " or t.userName like '%" + model.getUserName() + "%' ";
			} else {
				wheresql += "  t.userName like '%" + model.getUserName() + "%' ";
			}

		}
		// 按显示名称
		if (null != model.getUserShowName() && !"".equals(model.getUserShowName())) {
			if (wheresql.length() > 3) {
				wheresql += " or t.userShowName like '%" + model.getUserShowName() + "%' ";
			} else {
				wheresql += " t.userShowName like '%" + model.getUserShowName() + "%' ";
			}

		}
		// 按英文名称
		if (null != model.getUserEngListName() && !"".equals(model.getUserEngListName())) {
			if (wheresql.length() > 3) {
				wheresql += " or t.userEngListName like '%" + model.getUserEngListName() + "%' ";
			} else {
				wheresql += " t.userEngListName like '%" + model.getUserEngListName() + "%' ";
			}

		}
		// 按电话
		if (null != model.getTell() && !"".equals(model.getTell())) {
			if (wheresql.length() > 3) {
				wheresql += " or t.tell like '%" + model.getTell() + "%' ";
			} else {
				wheresql += " t.tell like '%" + model.getTell() + "%' ";
			}

		}

		if (wheresql.length() > 3) {
			hsql += " where (" + wheresql + " ) ";
		}
		String whereand = "";
		if (!"0".equals(userType1)) {
			if (whereand.length() > 3) {
				whereand += " or t.userType = ? ";
			} else {
				whereand += " t.userType = ? ";
			}
			list.add(userType1);
		}
		if (!"0".equals(userType2)) {
			if (whereand.length() > 3) {
				whereand += " or t.userType = ? ";
			} else {
				whereand += "  t.userType = ? ";
			}
			list.add(userType2);
		}
		if (wheresql.length() > 3) {
			hsql += " and ( " + whereand + " )";
		} else {
			hsql += " where ( " + whereand + " )";
		}

		Object[] obj = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			obj[i] = list.get(i);
		}

		@SuppressWarnings("unchecked")
		List<SysUser> list1 = (List<SysUser>) this.getList(hsql, obj, model.getStart(), model.getLimit(), orderby);
		int count = this.getListCount("select count(*) " + hsql, obj, "");
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", list1);
		map.put("sum", count);
		return map;
	}

	/**
	 * 删除用户
	 */
	@Override
	public int deleteUser(String allId) {
		// TODO Auto-generated method stub
		if (null == allId || "".equals(allId)) {
			return 0;
		} else {
			JSONArray jsonarr = JSONArray.fromObject(allId);
			for (int i = 0; i < jsonarr.size(); i++) {
				String table_id = jsonarr.getString(i);
				SysUser su = (SysUser) this.getT(new SysUser(), table_id);
				this.delete(su);
			}
		}
		return 1;
	}

	/**
	 * 启用和禁用用户
	 */
	@Override
	public int deleteDisUser(String allId, String flag) {
		// TODO Auto-generated method stub
		if (null == allId || "".equals(allId)) {
			return 0;
		} else {
			JSONArray jsonarr = JSONArray.fromObject(allId);
			for (int i = 0; i < jsonarr.size(); i++) {
				String table_id = jsonarr.getString(i);
				SysUser su = (SysUser) this.getT(new SysUser(), table_id);
				su.setState(flag);
				this.updateT(su);
			}
		}
		return 1;
	}

	/**
	 * 查询一个班的的所有教的老师
	 */
	@Override
	public Map getClassTeacher(Sys_Class model) {
		// TODO Auto-generated method stub
		// 这句sql中的t.note放的是中间表的year,也就是届
		// String opt =t.table_id, t.userName, t.userShowName,
		// t.userEngListName, t.userPwd, t.tell, t.userEmail, t.sex, t.age,
		// t.note, t.userType, t.userImage, t.teacherId, t.teacherType, t.state,
		// t.country, t.education, t.qualifications,t.showTime
		String opt = "t.table_id, t.userName, t.userShowName, t.userEngListName, t.userPwd, t.tell, t.userEmail, t.sex, t.age, t1.year, t.userType, t.userImage, t.teacherId, t.teacherType, t.state, t.country, t.education, t.qualifications ,t.showTime";
		String hsql = "from SysUser t left join t.listsuc t1  where t.userType ='2' and t1.sys_Class.table_id = ? ";
		Object[] obj = new Object[] { model.getTable_id() };
		@SuppressWarnings("unchecked")
		List<SysUser> list1 = (List<SysUser>) this.getList("select DISTINCT new com.dream.model.SysUser(" + opt + ") " + hsql, obj, model.getStart(), model.getLimit(), "");
		int count = this.getListCount("select count(*) " + hsql, obj, "");
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", list1);
		map.put("sum", count);
		return map;
	}

	/**
	 * 查询一个班里面，一起呆过的学生
	 */
	@Override
	public Map getClassStudent(Sys_Class model) {
		// TODO Auto-generated method stub
		// String opt =t.table_id, t.userName, t.userShowName,
		// t.userEngListName, t.userPwd, t.tell, t.userEmail, t.sex, t.age,
		// t.note, t.userType, t.userImage, t.teacherId, t.teacherType, t.state,
		// t.country, t.education, t.qualifications,t.showTime
		String opt = "t.table_id, t.userName, t.userShowName, t.userEngListName, t.userPwd, t.tell, t.userEmail, t.sex, t.age, t1.year, t.userType, t.userImage, t.teacherId, t.teacherType, t.state, t.country, t.education, t.qualifications,t.showTime ";

		String hsql = "from SysUser t left join t.listsuc t1  where t.userType ='1' and t1.sys_Class.table_id = ? ";
		Object[] obj = new Object[] { model.getTable_id() };
		@SuppressWarnings("unchecked")
		List<SysUser> list1 = (List<SysUser>) this.getList("select DISTINCT new com.dream.model.SysUser(" + opt + ")  " + hsql, obj, model.getStart(), model.getLimit(), "");
		int count = this.getListCount("select count(*) " + hsql, obj, "");
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", list1);
		map.put("sum", count);
		return map;
	}

	// 查询用户所有的班
	@Override
	public Map getStudentInfo(SysUser model, String state) {
		// TODO Auto-generated method stub
		String type = "";
		if ("".equals(model.getUserType()) || null == model.getUserType()) {
			type = "1";
		} else {
			type = model.getUserType();
		}
		String hsql = "from Sys_Class t left join t.listsuc t1  where t1.sysUser.userType ='" + type + "' and t1.sysUser.table_id = ? and t.type='4'";
		// 如果有状态，则表示查询是当前这个班在职的教师，其他的，都只是和自己同一年就可以了，也就是同一届的意思
		if (null != state && !"".equals(state)) {
			hsql += " and t1.state ='1' ";
		}
		Object[] obj = new Object[] { model.getTable_id() };
		@SuppressWarnings("unchecked")
		List list1 = this.getList("select t.table_id as table_id ,t1.year as year " + hsql, obj, model.getStart(), model.getLimit(), "");
		String str = "";// 哪些班

		for (int i = 0; i < list1.size(); i++) {
			Object[] obj1 = (Object[]) list1.get(i);
			if (i == list1.size() - 1) {
				str += "'" + obj1[0].toString() + "'";
			} else {
				str += "'" + obj1[0].toString() + "',";
			}
		}
		String str1 = "";// 哪些年
		for (int i = 0; i < list1.size(); i++) {
			Object[] obj1 = (Object[]) list1.get(i);
			if (i == list1.size() - 1) {
				str1 += "'" + obj1[1].toString() + "'";
			} else {
				str1 += "'" + obj1[1].toString() + "',";
			}
		}
		if (null == list1 || list1.size() <= 0) {
			Map map = new HashMap();
			map.put("count", 0);
			map.put("list", null);
			map.put("sum", 0);
			return map;
		}
		String sql = "from SysUser t left join t.listsuc t1  where t.userType='2'  and t1.sys_Class.table_id in (" + str + ") and t1.year in (" + str1 + ")";
		List<SysUser> list2 = (List<SysUser>) this.getList("select t " + sql, model.getStart(), model.getLimit(), "");
		int count = this.getListCount("select count(*) " + sql, "");
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", list2);
		map.put("sum", count);
		return map;
	}

	/**
	 * 查询用户的基本个人信息
	 */
	@Override
	public SysUser getStudentBaicInfo(SysUser model) {
		// TODO Auto-generated method stub
		String hql = "from SysUser t";
		if (null == model || null == model.getTable_id() || "".equals(model.getTable_id())) {
			return null;
		}
		SysUser su = (SysUser) this.getT(new SysUser(), model.getTable_id());
		return su;

	}

	/**
	 * 用户登陆
	 */
	@Override
	public int getUserlogin(SysUser model, HttpServletRequest request) {
		// TODO Auto-generated method stub
		String hql = "from SysUser t where t.userType= ? and t.tell = ? and t.userPwd = ? and t.state='1' ";
		Object obj[] = new Object[] { model.getUserType(), model.getTell(), model.getUserPwd() };

		List<SysUser> sulist = this.getList(hql, obj, 0, 0, "");
		if (null == sulist || sulist.size() <= 0) {
			return 0;
		} else {
			request.getSession().setAttribute("sysUser", sulist.get(0));
		}

		return 1;
	}

	/**
	 * 查询老师教过的班
	 */
	@Override
	public Map getTeacherClass(SysUser model) {
		// TODO Auto-generated method stub
		// String opt
		// ="t.table_id, t.name, t.cid, t.cpid, t.type, t.lv, t.note, t.timeLearn, t.addresss, t.cpassword, t.open, t.pName,t.showTime,t.string1,t.string2";
		// 这里把中间表的年代和状态，放在了string1,string2之中
		// 其实这里的note字段，放入的是班级的ID,前台注意
		String opt = "t1.table_id, t.name, t.cid, t.cpid, t.type, t.lv, t.table_id, t.timeLearn, t.addresss, t.cpassword, t.open, t.pName,t.showTime,t1.year,t1.state";
		String sql = "from Sys_Class t left join t.listsuc t1  where t.type='4' and t1.sysUser.table_id = ? ";
		Object[] obj = new Object[] { model.getTable_id() };
		List<Sys_Class> list2 = (List<Sys_Class>) this.getList("select new com.dream.model.Sys_Class(" + opt + ") " + sql, obj, model.getStart(), model.getLimit(), "");
		int count = this.getListCount("select count(*) " + sql, obj, "");
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", list2);
		map.put("sum", count);
		return map;

	}

	/**
	 * 查询学生上过的班
	 */
	@Override
	public Map getStudentClass(SysUser model) {
		// String opt
		// ="t.table_id, t.name, t.cid, t.cpid, t.type, t.lv, t.note, t.timeLearn, t.addresss, t.cpassword, t.open, t.pName,t.showTime,t.string1,t.string2";
		// 这里把中间表的年代和状态，放在了string1,string2之中
		String opt = "t.table_id, t.name, t.cid, t.cpid, t.type, t.lv, t.note, t.timeLearn, t.addresss, t.cpassword, t.open, t.pName,t.showTime,t1.year,t1.state";
		String sql = "from Sys_Class t left join t.listsuc t1  where t.type='4' and t1.sysUser.table_id = ? ";
		Object[] obj = new Object[] { model.getTable_id() };
		List<Sys_Class> list2 = (List<Sys_Class>) this.getList("select new com.dream.model.Sys_Class(" + opt + ") " + sql, obj, model.getStart(), model.getLimit(), "");
		int count = this.getListCount("select count(*) " + sql, obj, "");
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", list2);
		map.put("sum", count);
		return map;
	}

	@Override
	public Map getStudent_Teacher_C(SysUser model) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 得到一个一个班的学生
	 */
	@Override
	public Map getTeacherClassStudent(Sys_Class model, String year) {
		// TODO Auto-generated method stub
		String sql = " from SysUser t left join t.listsuc t1 where t1.sys_Class.cid = ? and t1.year = ? and t.userType ='1' ";
		Object[] obj = new Object[] { model.getCid(), year };
		List<SysUser> sulist = (List<SysUser>) this.getList("select t " + sql, obj, 0, 0, "");
		int count = this.getListCount("select count(*) " + sql, obj, "");
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", sulist);
		map.put("sum", count);
		return map;
	}

	@Override
	public SysUser getUserInfo(SysUser model) {
		// TODO Auto-generated method stub

		String hql = "from SysUser t where t.tell = ? and t.userPwd = ?";

		Object[] obj = new Object[] { model.getTell(), model.getUserPwd() };

		SysUser su = (SysUser) this.getListOne(hql, obj, "");

		return su;
	}

	@Override
	public SysUser byTellInfo(SysUser model) {
		// TODO Auto-generated method stub
		String hql = "from SysUser t where t.tell = ? ";

		Object[] obj = new Object[] { model.getTell() };

		SysUser su = (SysUser) this.getListOne(hql, obj, "");

		return su;
	}

	@Override
	public List<Sys_LearnPlant> getByKeMu(SysUser model) {
		// TODO Auto-generated method stub
		String hql = "from Sys_LearnPlant t where t.pltype = 1 ";
		List<Sys_LearnPlant> list = this.getList(hql, 0, 100, "");

		return list;
	}

	@Override
	public List getListClassRoom(SysUser model) {
		// TODO Auto-generated method stub
		String type = "";
		if ("".equals(model.getUserType()) || null == model.getUserType()) {
			type = "1";
		} else {
			type = model.getUserType();
		}
		String hsql = "from Sys_Class t left join t.listsuc t1  where t1.sysUser.userType ='" + type + "' and t1.sysUser.table_id = ? and t.type='4'";
		// 如果有状态，则表示查询是当前这个班在职的教师，其他的，都只是和自己同一年就可以了，也就是同一届的意思
		hsql += " and t1.state ='1' ";
		Object[] obj = new Object[] { model.getTable_id() };
		@SuppressWarnings("unchecked")
		List list1 = this.getList("select t " + hsql, obj, model.getStart(), model.getLimit(), "");
		return list1;
	}

	@Override
	public Map getListLv(String str) {
		// TODO Auto-generated method stub
		Map map = new HashMap();
		String hql = "from Sys_Class t where t.cpid = ? ";
		Object[] obj = new Object[] { str };
		List<Sys_Class> list1 = this.getList(hql, obj, 0, 1000, "");
		for (int i = 0; i < list1.size(); i++) {
			list1.get(i).setTimeLearn("");
		}
		map.put("count", list1.size());
		map.put("list", list1);
		map.put("sum", list1.size());
		return map;
	}

	/**
	 * 
	 * 查询每一级的备课计划
	 */
	@Override
	public Map getListLvKeMu(String str) {
		Map map = new HashMap();
		String hql = "from Sys_LearnPlant t where t.plpid = ? ";
		Object[] obj = new Object[] { str };
		List list1 = this.getList(hql, obj, 0, 1000, "");
		map.put("count", list1.size());
		map.put("list", list1);
		map.put("sum", list1.size());
		return map;
	}

	/**
	 * 
	 * 查询专家标签的每一级
	 */
	@Override
	public Map getZJlable(String str) {
		Map map = new HashMap();
		String hql = "from Sys_Lable t where t.lablePid = ? ";
		Object[] obj = new Object[] { str };
		List list1 = this.getList(hql, obj, 0, 1000, "");
		map.put("count", list1.size());
		map.put("list", list1);
		map.put("sum", list1.size());
		return map;
	}

	@Override
	public List getMyLable(SysUser model) {
		// TODO Auto-generated method stub
		Map map = new HashMap();
		String hql = "from Sys_Lable t where t.sysUser.table_id = ? ";
		Object[] obj = new Object[] { model.getTable_id() };
		List list1 = this.getList(hql, obj, 0, 10000, "");
		return list1;
	}

	@Override
	public Sys_Lable getLableBylableId(String str) {
		// TODO Auto-generated method stub
		Map map = new HashMap();
		String hql = "from Sys_Lable t where t.lableId = ? ";
		Object[] obj = new Object[] { str };
		Sys_Lable list1 = (Sys_Lable) this.getListOne(hql, obj, "");
		return list1;
	}

	@Override
	public int addStudentFen(List<SysUser> listu, List<Sys_Lable> listl, int fen) {
		// TODO Auto-generated method stub
		if (listu == null || listl == null) {
			return 0;
		}
		for (int i = 0; i < listu.size(); i++) {
			for (int j = 0; j < listl.size(); j++) {
				Sys_User_Lable sul = new Sys_User_Lable();
				sul.setSysUser(listu.get(i));
				sul.setSys_Lable(listl.get(j));
				sul.setLableValue(fen);
				this.addT(sul);
			}
		}

		return 1;
	}

	@Override
	public SysUser addUser1(SysUser model) {
		// TODO Auto-generated method stub

		String hql = "from SysUser t where t.userName =  ? or t.tell = ? ";
		Object[] object = new Object[] { model.getUserName(), model.getTell() };
		@SuppressWarnings("unchecked")
		List<SysUser> list = this.getList(hql, object, 0, 0, "");
		if (null == list || list.size() <= 0) {
			model.setShowTime(Common.getTime());
			model.setCreateTime(Common.getTime1());
			this.addT(model);
			return null;
		} else {
			if (null == model.getTable_id() || "".equals(model.getTable_id())) {
				// 证明有相同名称和电话的
				return null;
			} else {

				for (int i = 0; i < list.size(); i++) {
					SysUser su = list.get(i);
					if (su.getTable_id().equals(model.getTable_id())) {
						// su.setDesc(model.getDesc());
						su.setNote(model.getNote());
						su.setSex(model.getSex());
						// su.setTeacherId(model.getTeacherId());
						// su.setTeacherType(model.getTeacherType());
						su.setTell(model.getTell());
						su.setUserEmail(model.getUserEmail());
						su.setUserEngListName(model.getUserEngListName());
						if (null != model.getUserImage()) {
							su.setUserImage(model.getUserImage());
						}
						su.setUserName(model.getUserName());
						su.setUserShowName(model.getUserShowName());
						su.setUserType(model.getUserType());
						su.setUserPwd(model.getUserPwd());

						this.updateT(su);
						return su;
					}
				}
			}
		}

		return null;
	}

	@Override
	public List<SysUser> getClassTeacherCurrrt(Sys_Class model) {
		// TODO Auto-generated method stub
		String opt = "t.table_id, t.userName, t.userShowName, t.userEngListName, t.userPwd, t.tell, t.userEmail, t.sex, t.age, t1.year, t.userType, t.userImage, t.teacherId, t.teacherType, t.state, t.country, t.education, t.qualifications ,t.showTime";
		String hsql = "from SysUser t left join t.listsuc t1  where t.userType ='2' and t1.sys_Class.table_id = ? ";
		Object[] obj = new Object[] { model.getTable_id() };
		@SuppressWarnings("unchecked")
		List<SysUser> list1 = (List<SysUser>) this.getList("select DISTINCT new com.dream.model.SysUser(" + opt + ") " + hsql, obj, model.getStart(), model.getLimit(), "");
		return list1;
	}

	@Override
	public JSONArray getUserLable(String user_table_id) {
		// TODO Auto-generated method stub
		String hql = "from Sys_Lable t left join t.listsul t1 where t1.sysUser.table_id = ?";
		String opt = "t.table_id, t.name,  SUM(t1.lableValue), t.type, t.jiaocai, t.lableId, t.lablePid, t1.sysUser.userName,t1.sysUser.table_id,t.showTime ";
		Object object[] = new Object[] { user_table_id };
		String opt1 = "select  " + opt + hql;
		List<Sys_Lable> list = this.getList(opt1, object, 0, 1000, " group by t.name ");
		if (list != null) {
			JsonConfig jc = new JsonConfig();

			jc.setExcludes(JsonString.sys_lable);
			JSONArray sjon = JSONArray.fromObject(list);

			return sjon;
		}
		return null;
	}
}
